﻿using System;
using MF.Networker;

namespace MF.Networker
{
    public class PacketLoader : IPacketLoader
    {
        private  const int maxBody = 1024;
        private  const int headLength = 10;
        public static readonly PacketLoader Default = new PacketLoader();

        #region IPacketLoader 成员

        public int GetBodyLength(byte[] head)
        {
            return BitConverter.ToInt32(head, 4);
        }

        public int HeadLength
        {
            get
            {
                return headLength;
            }
        }

        public int MaxBodyLength
        {
            get
            {
                return maxBody;
            }
        }

        public IPacket Parse(byte[] data)
        {
            int type = BitConverter.ToInt32(data, 0);
            Packet chatPacket = new Packet((PacketType)type);
            int length = BitConverter.ToInt32(data, 4);
            chatPacket.OptionOne = data[8];
            chatPacket.OptionTwo = data[9];
            chatPacket.WriteBytes(data, 10, data.Length - 10);
            chatPacket.Flip();

            return chatPacket;
        }

        #endregion
    }
}
